package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Text;
	
	public class Rupee extends Entity
	{
		[Embed(source = 'images/red_rupee.gif')] private const RED_RUPEE:Class;
		[Embed(source = 'images/blue_rupee.gif')] private const BLUE_RUPEE:Class;
		[Embed(source = 'fonts/score.ttf', embedAsCFF = "false", fontFamily = 'RUPEE_FONT')]
		private const RUPEE_FONT:Class;
		
		private var red_rupee:Image = new Image(RED_RUPEE);
		private var blue_rupee:Image = new Image(BLUE_RUPEE);
		
		private var y_velocity:int;
		private var rupee_count:int;
		private var rupee_state:int;
		private var points:int;
		private var alpha_count:int;
		private var theText:Text;
		
		public function Rupee() 
		{
			Text.font = "RUPEE_FONT";
			graphic = red_rupee;
			setHitbox(13, 25);
			layer = 0;
			y = -25
			rupee_state = 0;
			rupee_count = 50;
			theText = new Text("");
		}
	//Update	
		override public function update():void {
			if (rupee_count == 0 && rupee_state == 0) addRupee();
			if (rupee_state == 1) moveRupee();
			if (rupee_state == 2) moveText();
			if (rupee_state == 0) rupee_count--;
		}
	//Rupee Functions	
		private function addRupee():void {//ADD RUPEE
			var r_x:int = Math.random() * 482;
			x = r_x + 5
			y = -25;
			y_velocity = 0;
			var r_type:int = Math.random() * 10;
			if (r_type > 6) {
				graphic = red_rupee;
				type = "red rupee";
				points = 500;
			} else {
				graphic = blue_rupee;
				type = "blue rupee";
				points = 250;
			}
			rupee_state = 1;
		}
		private function moveRupee():void {//MOVE RUPEE
			y_velocity++;
			if (y_velocity > 20) y_velocity = 20;
			y = y + y_velocity;
			//falls off screen
			if (y > 600) {
				rupee_state = 0;
				rupee_count = (Math.random() * 100) + 1;
			}
			//intersects girder
			if (collide("girder", x, y)) {
				fracasWorld.score = fracasWorld.score + points;
				rupee_state = 2;
				theText.text = "+" + points.toString();
				if (type == "blue rupee") theText.color = 0x6888FF;
				if (type == "red rupee") theText.color = 0xFFA044;
				graphic = theText;
				alpha_count = 30;
			}
		}
		private function moveText():void {//MOVE TEXT
			theText.alpha = (alpha_count / 30.0);
			y = y - 3;
			alpha_count--;
			if (alpha_count==0) {
				rupee_state = 0;
				rupee_count = (Math.random() * 250) + 50;
				graphic = null;
			}
		}
	}

}